/*
    This source file is part of GeNa project.
    Copyright © GeNa project 2008
    For the latest info, see http://code.google.com/p/genaproject/ or  http://lumpyproject.forums-rpg.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef MAP_SERIALIZATION_H_INCLUDED
#define MAP_SERIALIZATION_H_INCLUDED


#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
using namespace boost::serialization;

#include "map.h"
#include "brush.h"
#include "model.h"

template<class Archive> void Map::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_NVP(groups);

    ar & BOOST_SERIALIZATION_NVP(models);

    if( Archive::is_saving::value)
    {
        std::vector<string> v;
        v.reserve(included_map.size());
        for( std::list<Map*>::iterator iter = included_map.begin(); iter != included_map.end(); iter++)
            v.push_back((*iter)->filename);
        ar & make_nvp("included_map", v);
    }
    else
    {

        for( std::list<Model*>::iterator iter = models.begin(); iter != models.end(); iter++)
            (*iter)->load(this);

        std::vector<string> v;
        ar & make_nvp("included_map", v);

        for( unsigned int i=0; i<v.size(); i++)
        {
            Map* map = mapManagerInstance.load(v[i],mapPath.branch_path());
            if( map)
                included_map.push_back(map);
        }
    }
}

template<class Archive> void Edge::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_NVP(vertex1);
    ar & BOOST_SERIALIZATION_NVP(vertex2);

    ar & BOOST_SERIALIZATION_NVP(face1);
    ar & BOOST_SERIALIZATION_NVP(face2);
}

template<class Archive> void Face::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_NVP(edge_loop);
    ar & BOOST_SERIALIZATION_NVP(vertices);
}

template<class Archive> void Vertex::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_NVP(vertex);
    ar & BOOST_SERIALIZATION_NVP(faces);
    ar & BOOST_SERIALIZATION_NVP(edges);
}

template<class Archive> void Brush::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_NVP(vertices);
    ar & BOOST_SERIALIZATION_NVP(edges);
    ar & BOOST_SERIALIZATION_NVP(faces);

    ar & BOOST_SERIALIZATION_NVP(map);
}

template<class Archive> void Group::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_NVP(brushes);
}

template<class Archive> void Model::serialize(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_NVP(name);
    ar & BOOST_SERIALIZATION_NVP(position);
    ar & BOOST_SERIALIZATION_NVP(orientation);
}

#endif // MAP_SERIALIZATION_H_INCLUDED
